package com.zoweunion.mechanic.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.order.CGOrderService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.TimeOutLevel;
import com.zoweunion.mechanic.util.certificate.OperationCertificate;
import com.zoweunion.mechanic.util.certificate.OperationCertificateOrder;
import com.zoweunion.mechanic.util.certificate.OperationCertificateOrderDetail;
import com.zoweunion.mechanic.util.certificate.OrderPay;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author xp
 * @date 2020年04月28日11:05 AM
 */
@RestController
@RequestMapping("/certificate")
@SuppressWarnings("unchecked")
public class CertificateOrderController extends BaseController {

    @Autowired
    private CGOrderService cgOrderService;


    /**
     * 插入操作证订单
     */
    @PostMapping("/insertCertificateOrder")
    public ResponseBean insertCertificateOrder(@RequestBody OperationCertificateOrder order,
                                               HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功",
                cgOrderService.insertCertificateOrder(order));
    }

    /**
     * 订单付款成功
     */
    @PostMapping("/insertCertificateOrderPay")
    public ResponseBean insertCertificateOrderPay(@RequestBody OrderPay order,
                                                  HttpServletRequest request) throws MyException {
        order.setCreateUser(getCurrentUser(request).getId());
        cgOrderService.insertCertificateOrderPay(order);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 修改订单信息
     */
    @PostMapping("/updateCertificateOrder")
    public ResponseBean updateCertificateOrder(@RequestBody OperationCertificateOrder order,
                                               HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());
        cgOrderService.updateCertificateOrder(order);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 审核通过/不通过
     */
    @PostMapping("/updateCertificateOrderStatus")
    public ResponseBean updateCertificateOrderStatus(@RequestBody OperationCertificateOrder order,
                                                     HttpServletRequest request) throws MyException {
        order.setCreateUser(getCurrentUser(request).getId());
        cgOrderService.updateCertificateOrderStatus(order);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 取消订单
     */
    @PostMapping("/deleteCertificateOrderStatus")
    public ResponseBean deleteCertificateOrderStatus(@RequestBody OperationCertificateOrder order,
                                                     HttpServletRequest request) throws MyException {
        cgOrderService.deleteCertificateOrderStatus(order);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 订单详情
     */
    @GetMapping("/selectCertificateOrderInfo")
    public ResponseBean selectCertificateOrderInfo(@RequestParam Map<String, Object> map,
                                                   HttpServletRequest request) throws MyException {
        OperationCertificateOrder certificate = cgOrderService.selectCertificateOrderInfo(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", certificate);
    }


    /**
     * 插入操作证
     */
    @PostMapping("/insertCertificate")
    public ResponseBean insertCertificate(@RequestBody OperationCertificate order,
                                          HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());

        return cgOrderService.insertCertificate(order);
    }

    /**
     * 删除操作证
     */
    @PostMapping("/deleteCertificate")
    public ResponseBean deleteCertificate(@RequestBody OperationCertificate order,
                                          HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());
        cgOrderService.deleteCertificate(order);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 订单列表
     */
    @GetMapping("/selectCertificateBusiness")
    public ResponseBean selectCertificateBusiness(@RequestParam Map<String, Object> map,
                                                  HttpServletRequest request) throws MyException {
        map.put("createUser", getCurrentUser(request).getId());
        PageInfo certificates = cgOrderService.selectCertificateBusiness(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", certificates);
    }

    /**
     * 操作证列表
     */
    @GetMapping("/selectCertificate")
    public ResponseBean selectCertificate(@RequestParam Map<String, Object> map,
                                          HttpServletRequest request) throws MyException {
        List<OperationCertificate> certificates = cgOrderService.selectCertificate(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", certificates);
    }

    /**
     * 更新操作证名称
     */
    @PostMapping("/updateCertificate")
    public ResponseBean updateCertificate(@RequestBody OperationCertificate order,
                                          HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());

        return cgOrderService.updateCertificate(order);
    }

    /**
     * 更新操作证物流信息
     */
    @PostMapping("/updateCertificateExpress")
    public ResponseBean updateCertificateExpress(@RequestBody OperationCertificateOrder order,
                                                 HttpServletRequest request) throws MyException {
        order.setSid(getCurrentUser(request).getS_id());
        order.setCreateUser(getCurrentUser(request).getId());
        return cgOrderService.updateCertificateExpress(order);
    }

    /**
     * 查询物流信息
     */

    @GetMapping("/logisticsExpress")
    public ResponseBean logisticsExpress(@RequestParam Map<String, Object> map,
                                         HttpServletRequest request) throws MyException {
        OperationCertificateOrder order = cgOrderService.logisticsExpress(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", order);
    }
}
